/*
 *	Shell for BCE
 *
 *	Copyright (C) 2011 XiaoJSoft Studio. All Rights Reserved.
 *	Copyright (C) Ji WenCong <whs_jwc@163.com>
 *
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include "include/main.h"
#include "include/strext.h"
#include "include/kernel.h"
#include "include/version.h"
#include "include/console/utils/atom.h"
#include "include/console/utils/molecule.h"
#include "include/console/cs_main.h"
#include "include/console/keybio.h"
#include "localization/local.h"

char* killSpace(char *szInput)
{
	int acCount;
	int acUsed;
	char *szIdx;
	char *szRet;

	collection *slcm;

	acCount = 0;
	for (szIdx = szInput; *szIdx != '\0'; szIdx++) {
		if (*szIdx == '{' || *szIdx == '[') {
			*szIdx = '(';
		}
		if (*szIdx == '}' || *szIdx == ']') {
			*szIdx = ')';
		}
		if (*szIdx != ' ') {
			acCount++;
		}
	}
	szRet = (char*) malloc((acCount + 1) * sizeof(char));
	if (szRet == NULL) {
		return(NULL);
	}
	*(szRet + acCount) = '\0';
	acUsed = 0;
	for (szIdx = szInput; *szIdx != '\0'; szIdx++) {
		if (*szIdx != ' ') {
			*(szRet + acUsed) = *szIdx;
			acUsed++;
		}
	}
	return(szRet);
}

int main(int argc, char *argv[], char *envp[])
{
	int enumElement;
	char *ptr;
	char *cssubin;
	int mode = 1;
	int silent = 0;
	char *ret;
	char *szInput;
	collection *slcm;
	collection *slcm_cmd;
	collection *slcm_sel;
	int subidx;
	if (argc <= 1) {
		printf(localization_table[35]);
		return(1);
	}
	while(argc > 1) {
		argv++;

		if (*argv == NULL) {
			break;
		}

		szInput = killSpace(*argv);

		if (szInput == NULL) {
			if (silent != 0) {
				printf(localization_table[1]);
			} else {
				printf(localization_table[2]);
			}
			goto rfsnext;
		}

		if (strlen_ext(szInput) == 0) {
			if (silent == 0) {
				printf(localization_table[17]);
			} else {
				printf(localization_table[2]);
			}
			goto rfsnext;
		}

		if (strcmp_ext(szInput, localization_table[3]) == 1 || strcmp_ext(szInput, localization_table[4]) == 1) {
			mode = 1;
			goto rfsnext;
		}

		if (strcmp_ext(szInput, localization_table[5]) == 1 || strcmp_ext(szInput, localization_table[6]) == 1) {
			mode = 0;
			goto rfsnext;
		}

		if (strcmp_ext(szInput, localization_table[7]) == 1 || strcmp_ext(szInput, localization_table[8]) == 1) {
			silent = 1;
			goto rfsnext;
		}

		if (strcmp_ext(szInput, localization_table[9]) == 1 || strcmp_ext(szInput, localization_table[10]) == 1) {
			silent = 0;
			goto rfsnext;
		}

		if (strcmp_ext(szInput, localization_table[36]) == 1 || strcmp_ext(szInput, localization_table[37]) == 1) {
			printf(localization_table[35]);
			goto rfsnext;
		}

		if (strcmp_ext(szInput, localization_table[11]) == 1) {
			printf("\n");
			goto rfsnext;
		}

		if (strcmp_ext(szInput, localization_table[64]) == 1) {
			while(!0) {
				printf(localization_table[41]);

				cssubin = inControl();

				for (ptr = cssubin; *ptr != '\0'; ptr++) {
					if (*ptr == '[' || *ptr == '{') {
						*ptr = '(';
					} else if (*ptr == ']' || *ptr == '}') {
						*ptr = ')';
					}
				}

				if (cssubin == NULL) {
					printf(localization_table[42]);
					break;
				}

				if (strlen(cssubin) == 0) {
					free(cssubin);
					continue;
				}

				if (strcmp(cssubin, localization_table[23]) == 0) {
					free(cssubin);
					break;
				}

				analysisMolecule(cssubin);

				free(cssubin);
			}
			goto rfsnext;
		}

		if (strcmp_ext(szInput, localization_table[65]) == 1) {
			while(!0) {
				printf(localization_table[45]);

				cssubin = inControl();

				if (cssubin == NULL) {
					printf(localization_table[42]);
					break;
				}

				for (ptr = cssubin; *ptr != '\0'; ptr++) {
					if (*ptr == '[' || *ptr == '{') {
						*ptr = '(';
					} else if (*ptr == ']' || *ptr == '}') {
						*ptr = ')';
					}
				}

				if (strlen(cssubin) == 0) {
					free(cssubin);
					continue;
				}

				if (strcmp(cssubin, localization_table[23]) == 0) {
					free(cssubin);
					break;
				}

				if (isNumeric(cssubin) == 1) {
					enumElement = atoi(cssubin);

					if (enumElement >= 0 && enumElement <= chemical_ecount) {
						traceElementInfo(enumElement, chemical_enames[enumElement], chemical_enames_eng[enumElement], chemical_heavy[enumElement], chemical_ionization_energy[enumElement], chemical_grstate_conf[enumElement], chemical_grstate_level[enumElement]);
					} else {
						printf(localization_table[46]);
					}
				} else {
					enumElement = atomExist(cssubin);

					if (enumElement != -1) {
						traceElementInfo(enumElement, chemical_enames[enumElement], chemical_enames_eng[enumElement], chemical_heavy[enumElement], chemical_ionization_energy[enumElement], chemical_grstate_conf[enumElement], chemical_grstate_level[enumElement]);
					} else {
						printf(localization_table[46]);
					}
				}

				free(cssubin);
			}

			goto rfsnext;
		}

#ifndef CROSS_COMPILER_WIN32
		if (strcmp_ext(szInput, localization_table[66]) == 1) {
			listElements(1, chemical_ecount);
			goto rfsnext;
		}
#endif
		slcm = sliceString(szInput, localization_table[70]);
		if (getCollectionCount(slcm) != 1) {
			slcm_cmd = getCollectionItem(slcm, 1);
			if (slcm_cmd != NULL) {
				if (strcmp_ext(slcm_cmd->string, localization_table[67]) == 1) {

					for (subidx = 2; subidx <= getCollectionCount(slcm); subidx++) {
						slcm_sel = getCollectionItem(slcm, subidx);
						if (slcm_sel != NULL) {
							printf(localization_table[69], slcm_sel->string);
							analysisMolecule(slcm_sel->string);
							printf("\n");
						}
						slcm_sel = NULL;
					}

					freeCollection(slcm);
					slcm = NULL;

					goto rfsnext;
				}

				if (strcmp_ext(slcm_cmd->string, localization_table[68]) == 1) {
					for (subidx = 2; subidx <= getCollectionCount(slcm); subidx++) {
						slcm_sel = getCollectionItem(slcm, subidx);
						if (slcm_sel != NULL) {
							if (isNumeric(slcm_sel->string) == 1) {
								enumElement = atoi(slcm_sel->string);

								if (enumElement >= 0 && enumElement <= chemical_ecount) {
									traceElementInfo(enumElement, chemical_enames[enumElement], chemical_enames_eng[enumElement], chemical_heavy[enumElement], chemical_ionization_energy[enumElement], chemical_grstate_conf[enumElement], chemical_grstate_level[enumElement]);
								} else {
									printf(localization_table[46]);
								}
							} else {
								enumElement = atomExist(slcm_sel->string);

								if (enumElement != -1) {
									traceElementInfo(enumElement, chemical_enames[enumElement], chemical_enames_eng[enumElement], chemical_heavy[enumElement], chemical_ionization_energy[enumElement], chemical_grstate_conf[enumElement], chemical_grstate_level[enumElement]);
								} else {
									printf(localization_table[46]);
								}
							}

							printf("\n");
						}
						slcm_sel = NULL;
					}

					freeCollection(slcm);
					slcm = NULL;

					goto rfsnext;
				}
			}

			slcm_cmd = NULL;
		}

		freeCollection(slcm);
		slcm = NULL;

		if (strcmp_ext(szInput, localization_table[12]) == 1) {
#ifdef VERSION_RC
			if (ver_rc == 0) {
				printf(localization_table[14], ver_major, ver_minor, ver_revision, ver_code_name);
			} else {
				printf(localization_table[15], ver_major, ver_minor, ver_revision, ver_rc, ver_code_name);
			}
#else
			printf(localization_table[16], ver_major, ver_minor, ver_revision, ver_code_name);
#endif
			goto rfsnext;
		}

		if (strcmp_ext(szInput, localization_table[13]) == 1) {
			csm_start(&silent, &mode);
			goto rfsnext;
		}

		ret = balanceCE(szInput, mode);
		if (ret != NULL) {
			if (silent == 0) {
				printf(localization_table[18]);
			}
			printf("%s\n", ret);
		} else {
			if (silent == 0) {
				printf(localization_table[19]);
			} else {
				printf(localization_table[2]);
			}
		}

rfsnext:
		free(szInput);
		argc--;
	}
	return(0);
}

